package ui.fragment.group_management;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.trek2000.android.enterprise.Enterprise;
import com.trek2000.android.enterprise.R;

import java.util.ArrayList;

import adapter.group_management.AddMemberToGroupAdapter;
import controller.company.SelectCompanyAsync;
import controller.user_management.AllTabInUserManagementFragmentAsync;
import ui.activity.login.Login;
import ui.fragment.user_management.AllTabInUserManagementFragment;
import utils.Utils;

/**
 * Created by Dong Le on 6/19/2015.
 */
public class AddMemberToGroupFragment extends Fragment implements View.OnClickListener{
    //String session
    public static boolean IS_ADD_GROUP_MANAGER = false;
    //Views session
    public static ImageButton mIbtnBackInAddMemberToGroupFragment;
    public static ListView mLsInAddMemberToGroupFragment;
    public static TextView mTvAddInAddMemberToGroupFragment;
    public static AddMemberToGroupAdapter addMemberToGroupAdapter;

    //The others session
    public static Context mContext;

    public static Fragment newInstance( boolean is_add_group_manager) {
        IS_ADD_GROUP_MANAGER = is_add_group_manager;
        AddMemberToGroupFragment fragment = new AddMemberToGroupFragment();
        return fragment;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ibtn_back_in_add_member_to_group_fragment:
                if (getActivity().getSupportFragmentManager().getBackStackEntryCount()
                        > 0) {
                    Log.i("","popBackStack ");
                    Fragment mFragmentCurrent = getActivity()
                            .getSupportFragmentManager().findFragmentById(
                                    R.id.fl_in_create_group_activity);
                    if (mFragmentCurrent != null) {
                        if(((Object)mFragmentCurrent).getClass().getSimpleName().equals
                                (AddMemberToGroupFragment.class.getSimpleName())){
                            getActivity().getSupportFragmentManager().popBackStack();
                        }
                    }
                }
                break;
            case R.id.tv_add_in_add_member_to_group_fragment:
                //save data to single ton
                if (IS_ADD_GROUP_MANAGER) {
                    Enterprise.groupManagement.setmAlPosOfItemCheckedInAddGroupManager
                            (AddMemberToGroupAdapter.mAlPosOfItemCheckedInAddGroupManager);
                } else {
                    Enterprise.groupManagement.setmAlPosOfItemCheckedInAddMemberToGroup(
                            AddMemberToGroupAdapter.mAlPosOfItemCheckedInAddMemberToGroup);
                }

                if (getActivity().getSupportFragmentManager().getBackStackEntryCount()
                        > 0) {
                    Fragment mFragmentCurrent = getActivity()
                            .getSupportFragmentManager().findFragmentById(
                                    R.id.fl_in_create_group_activity);
                    if (mFragmentCurrent != null) {
                        if(((Object)mFragmentCurrent).getClass().getSimpleName().equals
                                (AddMemberToGroupFragment.class.getSimpleName())){
                            getActivity().getSupportFragmentManager().popBackStack();
                        }
                    }
                }
                break;
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = getLayoutInflater(savedInstanceState).inflate(
                R.layout.fragment_add_member_to_group, container, false);

        initialViews(v);
        initialData();
        getDataForAddMemberToGroupFragment();
        mContext = getActivity();
        return  v;
    }


    private void initialData() {
        mIbtnBackInAddMemberToGroupFragment.setOnClickListener(this);
        mTvAddInAddMemberToGroupFragment.setOnClickListener(this);
        addMemberToGroupAdapter = new AddMemberToGroupAdapter(
                getActivity(), R.layout.simple_list_item_in_add_member_to_group_fragment,
                AllTabInUserManagementFragmentAsync.mAlAllTabInUserManagement,
                IS_ADD_GROUP_MANAGER);
        mLsInAddMemberToGroupFragment.setAdapter(addMemberToGroupAdapter);
        //enable ibtn back, avoid not load view in create group fragment, if user click back so fast.
        mIbtnBackInAddMemberToGroupFragment.setEnabled(false);

    }

    private void initialViews(View v) {
        mIbtnBackInAddMemberToGroupFragment = (ImageButton) v.findViewById(
                R.id.ibtn_back_in_add_member_to_group_fragment);
        mLsInAddMemberToGroupFragment = (ListView) v.findViewById(
                R.id.lv_in_add_member_to_group_fragment);
        mTvAddInAddMemberToGroupFragment = (TextView) v.findViewById(
                R.id.tv_add_in_add_member_to_group_fragment);
    }

    private void getDataForAddMemberToGroupFragment() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
            new AllTabInUserManagementFragmentAsync(getActivity()).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
                    String.valueOf(Login.user.getUserInfo().getUserGlobalId()),
                    String.valueOf(Login.user.getUserInfo().getAccessToken()),
                    String.valueOf(
                            Enterprise.company.getCompaniesOfUserInfoSelected().getCompanyId()),
                    String.valueOf(
                            Enterprise.company.getCompaniesOfUserInfoSelected().getCompanyIdGroup()));
        else
            new AllTabInUserManagementFragmentAsync(getActivity()).execute(
                String.valueOf(Login.user.getUserInfo().getUserGlobalId()),
                String.valueOf(Login.user.getUserInfo().getAccessToken()),
                String.valueOf(
                        Enterprise.company.getCompaniesOfUserInfoSelected().getCompanyId()),
                String.valueOf(
                        Enterprise.company.getCompaniesOfUserInfoSelected().getCompanyIdGroup()));
    }

    public static void setTextForTextViewAddInAddMemberToGroupFragment(ArrayList <Integer> mAlPosOfItemChecked, Boolean is_add_group_manager) {
        if(is_add_group_manager) {
            if (mAlPosOfItemChecked.size() > 0) {
                mTvAddInAddMemberToGroupFragment.setEnabled(true);
                mTvAddInAddMemberToGroupFragment.setTextColor(mContext.getResources().getColor(R.color.orange));
                AddMemberToGroupFragment.mTvAddInAddMemberToGroupFragment.setText(mContext.getString(R.string.add) + "(" +
                                String.valueOf(mAlPosOfItemChecked.size()) + ")"
                );
            } else {
                mTvAddInAddMemberToGroupFragment.setEnabled(false);
                mTvAddInAddMemberToGroupFragment.setTextColor(mContext.getResources().getColor(R.color.gray));
                AddMemberToGroupFragment.mTvAddInAddMemberToGroupFragment.setText(mContext.getString(R.string.add));
            }
        }else {
            if (mAlPosOfItemChecked.size() > 0) {
                mTvAddInAddMemberToGroupFragment.setEnabled(true);
                mTvAddInAddMemberToGroupFragment.setTextColor(mContext.getResources().getColor(R.color.orange));
                AddMemberToGroupFragment.mTvAddInAddMemberToGroupFragment.setText(mContext.getString(R.string.add) + "(" +
                                String.valueOf(mAlPosOfItemChecked.size()) + ")"
                );
            } else {
                mTvAddInAddMemberToGroupFragment.setEnabled(false);
                mTvAddInAddMemberToGroupFragment.setTextColor(mContext.getResources().getColor(R.color.gray));
                AddMemberToGroupFragment.mTvAddInAddMemberToGroupFragment.setText(mContext.getString(R.string.add));
            }
        }
    }

}
